<?php

class Users extends Zend_Db_Table_Abstract
{
    protected $_name = 'users';
    protected $_rowClass = 'User';

	public function checkUsernameAndPassword( $username, $password )
	{
		$results = array();
		
		if(empty($username) || empty($password))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$row = $this->fetchRow("username='$username' and password='$password'");
			if($row)
			{
				$results['status'] = "SUCCEEDED";
				$results['rows'] = array( $row->toArray() );
			}
			else
			{
				$results['status'] = "FAILED_BAD_CREDENTIALS";
				$results['rows'] = array();
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['reason'] = ''.$e;
			$results['rows'] = array();
		}

		return $results;		
	}
	
	public function userNameExists( $username )
	{
        $rows = $this->fetchAll("username = '$username'");
		if($rows->count()<1) return false;
		return true;
	}
	
	public function createUser( $userProfile )
	{
		$results = array();
		
		if(null == $userProfile || !is_array($userProfile))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$r = $this->insert( $userProfile );
			if($r>=0)
			{
				$results['status'] = "SUCCEEDED";
				$results['rows_affected'] = 1;
				$results['user_id'] = $r;
			}
			else
			{
				$results['status'] = "FAILED";
				$results['rows_affected'] = 0;
				$results['user_id'] = -1;
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['rows_affected'] = 0;
			$results['user_id'] = -1;
		}
		
		return $results;
	}
 
 	public function updateUser( $userProfile, $VAS_user_id )
	{
		$results = array();
		
		if(null == $userProfile || !is_array($userProfile))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		if(!isset($VAS_user_id))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$r = $this->update( $userProfile, "id=$VAS_user_id" );
			if($r==1)
			{
				$results['status'] = "SUCCEEDED";
				$results['rows_affected'] = 1;
			}
			else
			{
				$results['status'] = "FAILED";
				$results['rows_affected'] = 0;
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}
	
 	public function updateFDMSFDUser( $userProfile, $FDMS_license_num )
	{
		$results = array();
		
		if(null == $userProfile || !is_array($userProfile))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		if(!isset($FDMS_license_num))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$r = $this->update( $userProfile, "FDMS_license_number='$FDMS_license_num'" );
			if($r==1)
			{
				$results['status'] = "SUCCEEDED";
				$results['rows_affected'] = 1;
			}
			else
			{
				$results['status'] = "FAILED";
				$results['rows_affected'] = 0;
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}

	public function getFDidsForLicense( $FDMS_license_num )
	{
		$results = array();
		
		if(!isset($FDMS_license_num))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$r = $this->fetchRow( "FDMS_license_number='$FDMS_license_num'" );
			if($r)
			{
				$results['status'] = "SUCCEEDED";
				$row = array();
				$row['FDids'] = $r->FDids;
				$results['rows'] = array( $row );
			}
			else
			{
				$results['status'] = "FAILED";
				$results['rows_affected'] = 0;
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}
	
	public function funeralDirExistsAtBranch( $license_num, $branch_number )
	{
        $rows = $this->fetchRow("FDMS_license_number = '$license_num'");
		if(null == $rows) 
			return false;
		
		$branches = explode( ",", $rows['branchnums'] );
		if(in_array($branch_number,$branches)) return true;
		return false;
	}
	
	public function addBranchInfoToFuneralDirector( $fdId, $branch_number, $FDMS_license_num )
	{
		$results = array();
		
		if(!isset($fdId))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		if(!isset($branch_number))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		if(!isset($FDMS_license_num))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$profile = $this->fetchRow("FDMS_license_number='$FDMS_license_num'");
			if(!$profile)
			{
				// error message and leave	
				$results['status'] = "FAILED_NO_SUCH_LICENSE";
				return $results;
			}
			$FDids_str = $profile['FD_ids']; $FDids = explode(",", $FDids_str);
			$branches_str = $profile['branchnums']; $branches = explode(",", $branches_str);
			
			if(!in_array( $fdId, $FDids )) $FDids[] = $fdId;

			if(!in_array( $branch_number, $branches )) $branches[] = $branch_number;
			
			$FDids_str = implode(",", $FDids);
			$branches_str = implode(",", $branches);
			
			$data['branchnums'] = $branches_str;
			$data['FD_ids'] = $FDids_str;
			$r = $this->update( $data, "FDMS_license_number='$FDMS_license_num'" );
			if($r==1)
			{
				$results['status'] = "SUCCEEDED";
				$results['rows_affected'] = 1;
			}
			else
			{
				$results['status'] = "FAILED";
				$results['rows_affected'] = 0;
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}

	public function removeBranchInfoFromFuneralDirector( $fdId, $branch_number, $FDMS_license_num )
	{
		$results = array();
		
		if(null == $userProfile || !is_array($userProfile))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		if(!isset($FDMS_license_num))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$profile = $this->fetchRow("FDMS_license_number='$FDMS_license_num'");
			if(!$profile)
			{
				// error message and leave	
				$results['status'] = "FAILED_NO_SUCH_LICENSE";
				return $results;
			}
			
			$FDids_str = $profile['FDids']; $FDids = explode(",", $FDids_str);
			$branches_str = $profile['branches']; $branches = explode(",", $branches_str);
			
			if(in_array( $fdId, $FDids )) 
				$FDids = array_diff( array($fdId), $FDids );
			
			if(in_array( $branch_number, $branches )) 
				$branches = array_diff( array($branch_number), $branches );
			
			$FDids_str = implode(",", $FDids);
			$branches_str = implode(",", $branches);
			
			$data['branches'] = $branches_str;
			$data['FDids'] = $FDids_str;
			
			$r = $this->update( $data, "FDMS_license_number='$FDMS_license_num'" );
			if($r==1)
			{
				$results['status'] = "SUCCEEDED";
				$results['rows_affected'] = 1;
			}
			else
			{
				$results['status'] = "FAILED";
				$results['rows_affected'] = 0;
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['rows_affected'] = 0;
		}
		
		return $results;
	}
	
	public function fetchAdminUsers()
	{
		$results = array();

		try
		{
			$fetched_rows = $this->fetchAll("role_name='Admin'");
			$rows = array();
			foreach($fetched_rows as $fetched_row)
				$rows[] = $fetched_row->toArray();
			$results['status'] = "SUCCEEDED";
			$results['rows'] = $rows;
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['status_reason'] = $e;
		}
		
		return $results;		
	}
	
	public function createAdministratorUser( $adminProfile )
	{
		$results = array();
		
		if(null == $adminProfile || !is_array($adminProfile))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		// take admin profile fields as is
		// make FD-specific fields -1 or empty string but not null
		$adminProfile["role_name"] = "Admin";
		$adminProfile["status"] = "ACTIVE";
		$adminProfile["FH_id"] = -1;
		$adminProfile["FDMS_license_number"] = "";
		$adminProfile["FD_ids"] = "";
		$adminProfile["branches"] = "";
		
		// call createUser()
		return $this->createUser( $adminProfile );
	}
	
	public function updateAdministratorUser( $adminProfile, $VAS_user_id )
	{
		$results = array();
		
		if(null == $adminProfile || !is_array($adminProfile))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		// fill in $adminProfile fields that are FD-specific with empty values
		return $this->updateUser( $adminProfile, $VAS_user_id );
	}
	
	public function funeralDirExistsWithLicenseNumber( $license_number )
	{
		$value = false;
				
		if(empty($license_number))
			return false;
		
		try
		{
			$row = $this->fetchRow("role_name='FD' and FDMS_license_number='$license_number'");
			if(null !== $row)
			{
				$value = true;
			}
			else
			{
				$value = false;
			}
		}
		catch(Exception $e)
		{
			$value = false;
		}

		return $value;
	}
	
	public function createFuneralDirectorUser( $fdProfile )
	{
		$results = array();
		
		if(null == $fdProfile || !is_array($fdProfile))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		$fdProfileToAdd = array();		
		$fdProfileToAdd["role_name"] = "FD";
		$fdProfileToAdd["status"] = "ACTIVE";
		$fdProfileToAdd["username"] = $fdProfile["username"];
		$fdProfileToAdd["password"] = $fdProfile["password"];
		$fdProfileToAdd["full_name"] = $fdProfile["full_name"];
		$fdProfileToAdd["fname"] = $fdProfile["fname"];
		$fdProfileToAdd["mname"] = $fdProfile["mname"];
		$fdProfileToAdd["lname"] = $fdProfile["lname"];
		$fdProfileToAdd["address"] = "";
		$fdProfileToAdd["city"] = "";
		$fdProfileToAdd["state"] = "";
		$fdProfileToAdd["zip"] = "";
		$fdProfileToAdd["phone"] = "";
		$fdProfileToAdd["email"] = "";
		$fdProfileToAdd["photo"] = "";
		$fdProfileToAdd["FD_ids"] = $fdProfile["FD_ids"];
		$fdProfileToAdd["branches"] = $fdProfile["branches"];
		$fdProfileToAdd["branchnums"] = $fdProfile["branchnums"];
		$fdProfileToAdd["FDMS_license_number"] = $fdProfile["FDMS_license_number"];
		
		
		//Zend_Debug::dump($fdProfileToAdd);
		
		// call createUser()
		return $this->createUser( $fdProfileToAdd );
	}
	
	public function updateFuneralDirectorUser( $fdProfile, $VAS_user_id )
	{
		$results = array();
		
		if(null == $fdProfile || !is_array($fdProfile))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		// fill in $fdProfile fields that are Admin-specific with empty values
		return $this->updateUser( $fdProfile, $VAS_user_id );
	}
	
	public function getFuneralDirectorForFDid( $FDMS_fd_id )
	{
	}
	
	public function getUser( $VAS_user_id )
	{
		$results = array();
		
		if(!isset($VAS_user_id))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$row = $this->fetchRow("id=$VAS_user_id");
			if($row)
			{
				$results['status'] = "SUCCEEDED";
				$results['rows'] = array( $row->toArray() );
			}
			else
			{
				$results['status'] = "FAILED";
				$results['rows'] = array();
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['rows'] = array();
		}
		
		return $results;
	}
	
	public function getFuneralDirectorForFDMSLicenseNum( $FDMS_license_num )
	{
		$results = array();
				
		if(strlen($FDMS_license_num)<1)
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$row = $this->fetchRow("FDMS_license_number='$FDMS_license_num'");
			if($row)
			{
				$results['status'] = "SUCCEEDED";
				$results['rows'] = array( $row->toArray() );
			}
			else
			{
				$results['status'] = "FAILED";
				$results['rows'] = array();
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['rows'] = array();
		}

		return $results;		
	}
	
	public function getModulesForFuneralDirector( $FDMS_license_num )
	{
		$results = array();
		
		if(empty($FDMS_license_num))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$row = $this->fetchRow("FDMS_license_number = '$FDMS_license_num'");
			if($row)
			{
				if(empty($row['modules']))
					$results['modules'] = VASProcessModules::getDefaultOrder();
				else	
					$results['modules'] = $row->modules;
					
				$results['status'] = "SUCCEEDED";
				$results['rows_affected'] = 1;
			}
			else
			{
				$results['status'] = "FAILED_NO_SUCH_LICENSE";
				$results['rows_affected'] = 0;
				$results['modules'] = "";
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['reason'] = "".$e;
		}
		
		return $results;
	}
	
	public function setModulesForFuneralDirector( $modules, $FDMS_license_num )
	{
		$results = array();
		
		if(empty($FDMS_license_num))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		if(empty($modules))
			$modules = VASProcessModules::getDefaultOrder();

		$profile['modules'] = $modules;

		try
		{
			$row = $this->update( $profile, "FDMS_license_number = '$FDMS_license_num'");
			$results['status'] = "SUCCEEDED";
			$results['rows_affected'] = $r;
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['reason'] = "".$e;
			Zend_Debug::dump($e);
			exit(0);
		}
		
		return $results;
	}
	
	public function getFuneralDirectorForVASid( $VAS_fd_id )
	{
	}
	
	public function getFuneralDirectorForFDMSId( $FDMS_fd_id )
	{
		$results = array();
				
		if(empty($FDMS_fd_id))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		$expr = "FD_ids = $FDMS_fd_id or (FD_ids REGEXP '.*,$FDMS_fd_id,') or (FD_ids REGEXP '^$FDMS_fd_id,.*') or (FD_ids REGEXP ',$FDMS_fd_id\$')";
		
		try
		{
			$row = $this->fetchRow("$expr");
			if($row)
			{
				$results['status'] = "SUCCEEDED";
				$results['rows'] = array( $row->toArray() );
			}
			else
			{
				$results['status'] = "FAILED";
				$results['rows'] = array();
			}
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['rows'] = array();
		}

		return $results;		
	}
	
	public function fetchFuneralDirectorsForBranch( $branch_number )
	{
		// select all FDs whose branches contain $branch_id
		$results = array();
		
		if(empty($branch_number))
		{
			$results['status'] = "FAILED_BAD_PARAM";
			return $results;
		}
		
		try
		{
			$where = "branchnums = '$branch_number' or branchnums REGEXP '.*,$branch_number$' or branchnums REGEXP '^$branch_number,.*' or branchnums REGEXP '.*,$branch_number,.*'";
			$fetchedFdRows = $this->fetchAll($where);
//			$fetchedFdRows = $this->fetchAll("branchnums REGEXP '$reg1' OR branchnums REGEXP '$reg2' OR branchnums REGEXP '$reg3'");
//			$fetchedFdRows = $this->fetchAll("branches LIKE '%$branch_id%'");
			$results['status'] = "SUCCEEDED";
			$results['rows'] = array();
			foreach( $fetchedFdRows as $row )
				$results['rows'][] = $row->toArray();
				
		}
		catch(Exception $e)
		{
			$results['status'] = "FAILED";
			$results['rows'] = array();
		}
		
		return $results;
	}
	}

?>
